package com.marco;

import java.util.HashMap;
import java.util.Map;

/**
 * GpsUtils.java
 * Create on 2017/4/7
 * Copyright(c) JD Inc 2017. All Rights Reserved.
 * <pre>
 * 功能说明：提供GPS坐标转火星坐标算法，以及GPS转百度坐标算法
 * </pre>
 */
public class GpsUtils {
    //地球长半轴
    private static double a = 6378245.0;
    //第一偏心率平方
    private static double ee = 0.00669342162296594323;

    //圆周率度
    private static Double PI = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * 由GPS转到火星坐标
     *
     * @param lat 纬度
     * @param lng 经度
     * @return Map< String,Double > [lat-纬度;lng-经度]
     */
    public static Map<String, Double> toGCJ(Double lng,Double lat) {
        lat = Double.parseDouble(String.format("%.5f", lat));
        lng = Double.parseDouble(String.format("%.5f", lng));
        Map<String, Double> resultMap = new HashMap<>();
        //如果在国外，则默认不进行转换
        if (isBelongToChina(lat, lng)) {
            resultMap.put("lat", lat);
            resultMap.put("lng", lng);
            return resultMap;
        }
        double dLat = transformLat(lng - 105.0, lat - 35.0);
        double dLon = transformLon(lng - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * Math.PI;//算出纬度弧度
        double magic = Math.sin(radLat);//算出角度
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
        resultMap.put("lat", lat + dLat);
        resultMap.put("lng", lng + dLon);
        return resultMap;
    }


    public static Map<String, Double> toBD09(Double lng, Double lat) {
        Map<String, Double> resultMap = toGCJ(lng, lat);
        Double x = resultMap.get("lng");
        Double y = resultMap.get("lat");
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
        resultMap.put("lng", z * Math.cos(theta) + 0.0065);
        resultMap.put("lat", z * Math.sin(theta) + 0.006);
        return resultMap;
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(x > 0 ? x : -x);
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x
                * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0
                * Math.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y
                * Math.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(x > 0 ? x : -x);
        ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x
                * Math.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0
                * Math.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x
                / 30.0 * Math.PI)) * 2.0 / 3.0;
        return ret;
    }

    private static boolean isBelongToChina(double lat, double lng) {
        if (lng < 72.004 || lng > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    /**
     * void bd_decrypt(double bd_lat, double bd_lon, double &gg_lat, double &gg_lon)
     {
     double x = bd_lon - 0.0065, y = bd_lat - 0.006;
     double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
     double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
     gg_lon = z * cos(theta);
     gg_lat = z * sin(theta);
     }
     */
}
